PROCEDURE IntToString(lt:LONGINT;VAR str:STRING); VAR lt2,len,i,ll,dd:LONGINT; sig:BOOLEAN; BEGIN IF lt<0 THEN sig:=TRUE ELSE sig:=FALSE END;lt2:=ABS(lt); IF lt2=0 THEN str:="0";RETURN; (* fastest way (log10 doesn't work at 10,100,... !)*) ELSIF lt2=10 THEN str:="10";ELSIF lt2=100 THEN str:="100"; ELSIF lt2=1000 THEN str:="1000";ELSIF lt2=10000 THEN str:="10000"; ELSIF lt2=100000 THEN str:="100000";ELSIF lt2=1000000 THEN str:="1000000"; ELSIF lt2=10000000 THEN str:="10000000";ELSIF lt2=100000000 THEN str:="100000000"; ELSIF lt2=1000000000 THEN str:="1000000000"; ELSE len:=ENTIER(ML.LOG10(lt2))+1;dd:=1; IF len>1 THEN i:=1; REPEAT dd:=dd*10; INC(i); UNTIL i>=len; ELSE str[0]:=gs(lt2); str[1]:='\o'; IF sig THEN ST.InsertChar(str,0,'-') END; RETURN; END; i:=0; REPEAT ll:=lt2 DIV dd; lt2:=lt2 MOD dd; dd:=dd DIV 10; str[i]:=gs(ll); INC(i); UNTIL i>=len; str[len]:='\o'; END; IF sig THEN ST.InsertChar(str,0,'-') END; END IntToString;
(* writes an INTEGER to stdout *)
PROCEDURE WriteInt(lt:LONGINT); VAR hs:STRING; BEGIN IntToString(lt,hs); io.WriteString(hs); END WriteInt;
(* converts REAL into ARRAY OF CHAR
(aim: no spaces, upto 4 digits after the point *)
PROCEDURE RealToString(rt:REAL;VAR str2:STRING); VAR rt2:REAL; sig:BOOLEAN; hs:STRING; lt1,lt2,lt3:LONGINT;BEGIN IF rt=0.0 THEN str2:="0";RETURN END; IF rt<0.0 THEN sig:=TRUE ELSE sig:=FALSE END; rt2:=ABS(rt); lt1:=ENTIER(rt2); IntToString(lt1,str2); rt2:=rt2-lt1; IF rt2>=0.00005 THEN rt2:=rt2*10000.0; lt2:=ENTIER(rt2+0.5); IF lt2>=9999 THEN INC(lt1); IntToString(lt1,str2); ELSE ST.Append(str2,"."); lt3:=1; IF (lt2 MOD 10)=0 THEN lt2:=lt2 DIV 10;lt3:=10; END; IF (lt2 MOD 10)=0 THEN lt2:=lt2 DIV 10;lt3:=100; END; IF (lt2 MOD 10)=0 THEN lt2:=lt2 DIV 10;lt3:=1000; END; IntToString(lt2,hs); lt2:=lt2*lt3; IF lt2<10 THEN ST.Append(str2,"000") ELSIF lt2<100 THEN ST.Append(str2,"00") ELSIF lt2<1000 THEN ST.Append(str2,"0") ELSE END; ST.Append(str2,hs); END; END; IF sig THEN IF (str2[0]='0') AND (ST.Length(str2)=1) THEN ELSE ST.InsertChar(str2,0,'-') END; END; END RealToString;
(* writes a REAL to stdout *)
PROCEDURE WriteReal(rt:REAL); VAR hs:STRING; BEGIN RealToString(rt,hs);io.WriteString(hs); END WriteReal;
(* returns a factor between 0.9 and 1.0 *)
PROCEDURE RND2*():REAL; BEGIN bb:=RND.RND(901)+100; bb:=bb/10000.0; IF RND.RND(2)=1 THEN RETURN 1.0+bb ELSE RETURN 1.0-bb END; END RND2;
(* generates a filename like "obj.0235" *)
PROCEDURE GenFn(str1:STRING;VAR str2:STRING;tt:INTEGER); VAR hs:STRING; BEGIN str2:=str1; IntToString(tt,hs); IF tt>=1000 THEN ST.Append(str2,".");ST.Append(str2,hs); ELSIF (tt<1000) AND (tt>=100) THEN ST.Append(str2,".0");ST.Append(str2,hs); ELSIF (tt<100) AND (tt>=10) THEN ST.Append(str2,".00");ST.Append(str2,hs); ELSIF tt<10 THEN ST.Append(str2,".000");ST.Append(str2,hs) END; END GenFn;